package SwingView.Quiz;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JOptionPane;

import JDBC.StudentQuizActivities;
import SwingController.CmsPresenter;
import SwingModel.CmsQuiz;

public class ALQuizTake implements ActionListener{

	private CmsQuiz quiz;
	
	public ALQuizTake(CmsQuiz quiz){
		this.quiz = quiz;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String time;
		
		
		//THERE IS TIME CONSTRAINT
		if(quiz.getDuration() > 0){
			long timeConstraint = quiz.getDuration();
			
			//Check time til the due date
			Date dueDate = quiz.getDueDate();
			Date today = new Date();
			
			//compare to due date
			if(dueDate.getTime() <= (today.getTime() + (timeConstraint * 60 * 1000)) ){ // TIME CONSTRAINT AND DUE DATE VIOLATE EACH OTHER
				timeConstraint = (dueDate.getTime() - today.getTime()) / 1000 / 60;
				
				String message = "You are about to take this quiz/test:\n\n " 
						+ "   - " + quiz.getQuizTitle() + "\n\n"
						+ "You have " + timeConstraint + " minutes to complete this quiz/test before\nthe due date at " + CmsPresenter.sdf.format(dueDate)
						+ "Do you want to start the quiz/test now?";
				int returnVal = JOptionPane.showConfirmDialog(CmsPresenter.mainView, message, "CONFIRMATION",JOptionPane.WARNING_MESSAGE);
				
				if(returnVal == JOptionPane.YES_OPTION){ // if user confirmed taking the quiz, show the quiz take page
					dueDate = quiz.getDueDate();
					today = new Date();
					
					if(today.before(dueDate)){
						StudentQuizActivities.setQuizStartDate(quiz.getQuizID(), CmsPresenter.user.getUsername(), new Date());
						CmsQuiz retrieveQuiz = StudentQuizActivities.retrieveQuiz( CmsPresenter.user.getSelectedCourse().getCourseID() , quiz.getQuizID());
						long duration = ( dueDate.getTime() - (new Date()).getTime() ) / 1000 / 60 ;
						retrieveQuiz.setDuration((int)duration);
						CmsPresenter.mainView.displayQuizTakePage(retrieveQuiz);
					}
					else{
						JOptionPane.showMessageDialog(CmsPresenter.mainView, "It's passed the duedate already. Cannot start the quiz/test.","ERROR", JOptionPane.ERROR_MESSAGE);
						CmsPresenter.mainView.displayQuizStudentPage(StudentQuizActivities.getListOfQuiz(CmsPresenter.user.getSelectedCourse().getCourseID()));
					}
				}
			}
			else{ // TIME CONSTRAINT AND DUE DATE DO NOT VIOLATE EACH OTHER
				timeConstraint = quiz.getDuration();
				String message = "You are about to take this quiz/test:\n\n " 
						+ "   - " + quiz.getQuizTitle() + "\n\n"
						+ "You have " + timeConstraint + " minutes to complete this quiz/test.\n"
						+ "Do you want to start the quiz/test now?";
				int returnVal = JOptionPane.showConfirmDialog(CmsPresenter.mainView, message, "CONFIRMATION",JOptionPane.WARNING_MESSAGE);
				
				if(returnVal == JOptionPane.YES_OPTION){ // if user confirmed taking the quiz, show the quiz take page
					StudentQuizActivities.setQuizStartDate(quiz.getQuizID(), CmsPresenter.user.getUsername(), new Date());
					CmsPresenter.mainView.displayQuizTakePage(StudentQuizActivities.retrieveQuiz(CmsPresenter.user.getSelectedCourse().getCourseID(), quiz.getQuizID()));
				}
			}
			
		}
		else{//THERE IS NO TIME LIMIT
			//Check time til the due date
			Date dueDate = quiz.getDueDate();
			Date today = new Date();
			long timeConstraint = (dueDate.getTime() - today.getTime()) / 1000 / 60;
			
			String message = "You are about to take this quiz/test:\n\n " 
					+ "   - " + quiz.getQuizTitle() + "\n\n"
					+ "There is NO TIME LIMIT for this quiz."
					+ "But you have " + timeConstraint + " minutes to complete this quiz/test before\nthe due date at " + CmsPresenter.sdf.format(dueDate)
					+ "Do you want to start the quiz/test now?";
			int returnVal = JOptionPane.showConfirmDialog(CmsPresenter.mainView, message, "CONFIRMATION",JOptionPane.WARNING_MESSAGE);
			
			if(returnVal == JOptionPane.YES_OPTION){ // if user confirmed taking the quiz, show the quiz take page
				dueDate = quiz.getDueDate();
				today = new Date();
				
				if(today.before(dueDate)){
					StudentQuizActivities.setQuizStartDate(quiz.getQuizID(), CmsPresenter.user.getUsername(), new Date());
					CmsQuiz retrieveQuiz = StudentQuizActivities.retrieveQuiz( CmsPresenter.user.getSelectedCourse().getCourseID() , quiz.getQuizID());
					long duration = ( dueDate.getTime() - (new Date()).getTime() ) / 1000 / 60 ;
					retrieveQuiz.setDuration((int)duration);
					CmsPresenter.mainView.displayQuizTakePage(retrieveQuiz);
				}
				else{
					JOptionPane.showMessageDialog(CmsPresenter.mainView, "It's passed the duedate already. Cannot start the quiz/test.","ERROR", JOptionPane.ERROR_MESSAGE);
					CmsPresenter.mainView.displayQuizStudentPage(StudentQuizActivities.getListOfQuiz(CmsPresenter.user.getSelectedCourse().getCourseID()));
				}
				
			}
		}
	}
}
